package com.personal.website.lottery.web.util;

import com.personal.website.lottery.table.*;
import com.personal.website.lottery.web.model.DoubleColorBall;
import com.personal.website.lottery.web.service.DoubleColorBallService;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Gavin on 2016/2/17.
 */
public class TableRecordsGenerate {

    private DoubleColorBallService doubleColorBallService;

    public TableRecordsGenerate(DoubleColorBallService doubleColorBallService) {
        this.doubleColorBallService = doubleColorBallService;
    }

    private List<Integer> getRedBalls(DoubleColorBall ball) {
        List<Integer> redBalls = new ArrayList<Integer>();
        redBalls.add(Integer.parseInt(ball.getRedOne()));
        redBalls.add(Integer.parseInt(ball.getRedTwo()));
        redBalls.add(Integer.parseInt(ball.getRedThree()));
        redBalls.add(Integer.parseInt(ball.getRedFour()));
        redBalls.add(Integer.parseInt(ball.getRedFive()));
        redBalls.add(Integer.parseInt(ball.getRedSix()));
        return redBalls;
    }

    public String generateBlueTableRecords() {
        return generatTableRecords(BlueTable.class);
    }

    public String generateEightDigitTableRecords() {
        return generatTableRecords(EightDigitTable.class);
    }

    public String generateElevenDigitTableRecords() {
        return generatTableRecords(ElevenDigitTable.class);
    }

    public String generateEnd3dTableRecords() {
        return generatTableRecords(End3dTable.class);
    }

    public String generateEndTableRecords() {
        return generatTableRecords(EndTable.class);
    }

    public String generateEvenTableRecords() {
        return generatTableRecords(EvenTable.class);
    }

    public String generateFourPart012TableRecords() {
        return generatTableRecords(FourPart012Table.class);
    }

    public String generateFourPartEnd012TableRecords() {
        return generatTableRecords(FourPartEnd012Table.class);
    }

    public String generateMainTableRecords() {
        return generatTableRecords(MainTable.class);
    }

    public String generateOddTableRecords() {
        return generatTableRecords(OddTable.class);
    }

    public String generatTableRecords(Class clazz) {
        String ballRecords = "";
        try {
            Method method = clazz.getMethod("newInstance", Integer.class, List.class, Integer.class);
            for (DoubleColorBall ball : this.doubleColorBallService.getBalls(30)) {
                ballRecords += method.invoke(null, ball.getIssue(), getRedBalls(ball), Integer.parseInt(ball.getBlue())).toString() + "$";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ballRecords.substring(0, ballRecords.length() - 1);
    }


}
